<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of OrdermanagementController
 *
 * @author Sorasak Siangsanan
 */
class OrdermanagementController extends Controller {

    private static function countMasterOrder() {
        $Criteria = new CDbCriteria();
        $Criteria->condition = "status_id = 4";
        $count = Order::model()->count($Criteria);

        return $count;
    }

    public function actionIndex() {
        $model = new Order('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Order'])) {
            $model->attributes = $_GET['Order'];
        }
        $this->render('index', array('model' => $model, 'orderMaster' => self::countMasterOrder()));
    }

    public function actionEdit() {
        $model = new Order('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Order'])) {
            $model->attributes = $_GET['Order'];
        }
        $this->render('edit', array('model' => $model, 'orderMaster' => self::countMasterOrder()));
    }

    public function actionForward() {

        $id = $_GET["id"];
        if (isset($id)) {
            $order = Order::model()->findByPk($id);
            if (isset($order) && $order->status_id == 3) {
                $order->status_id = 4;
                $order->update_date = date("Y-m-d H:i:s");
                $order->update();
            }
        }

        $this->redirect(array('ordermanagement/index'));
    }

    public function actionView() {

        $id = $_GET["id"];
        if (isset($id)) {
            $order = Order::model()->findByPk($id);
            if (!isset($order)) {
                $this->redirect(array('ordermanagement/index'));
            }
        } else {
            $this->redirect(array('ordermanagement/index'));
        }

        $this->render('vieworder', array('order' => $order));
    }

    public function actionForetellPrice() {
        $package_id = $_POST['package_id'];
        $data = Package::model()->findByPk($package_id);
        echo $data->price;
    }

    public function actionUpdate() {

        $id = $_GET["id"];
        if (isset($id)) {
            $order = Order::model()->findByPk($id);
            if (!isset($order)) {
                $this->redirect(array('ordermanagement/index'));
            }
        } else {
            $this->redirect(array('ordermanagement/index'));
        }

        $model = new OrdermanagementUpdateForm();

        if (isset($_POST['OrdermanagementUpdateForm'])) {
            $model->attributes = $_POST['OrdermanagementUpdateForm'];
            $model->time_of_birthday = $_POST['OrdermanagementUpdateForm']['time_of_birthday'];
            $model->profile_image_name = $_POST['OrdermanagementUpdateForm']['profile_image_name'];
            $model->foretell_type_1 = $_POST['OrdermanagementUpdateForm']['foretell_type_1'];
            $model->foretell_type_2 = $_POST['OrdermanagementUpdateForm']['foretell_type_2'];
            $model->foretell_type_3 = $_POST['OrdermanagementUpdateForm']['foretell_type_3'];
            $model->foretell_type_4 = $_POST['OrdermanagementUpdateForm']['foretell_type_4'];
            $model->foretell_type_5 = $_POST['OrdermanagementUpdateForm']['foretell_type_5'];
            $model->foretell_type_6 = $_POST['OrdermanagementUpdateForm']['foretell_type_6'];
            $model->foretell_type_7 = $_POST['OrdermanagementUpdateForm']['foretell_type_7'];
            $model->foretell_type_8 = $_POST['OrdermanagementUpdateForm']['foretell_type_8'];
            $model->foretell_type_9 = $_POST['OrdermanagementUpdateForm']['foretell_type_9'];
            $model->foretell_type_10 = $_POST['OrdermanagementUpdateForm']['foretell_type_10'];
            $model->foretell_type_11 = $_POST['OrdermanagementUpdateForm']['foretell_type_11'];
            $model->foretell_type_12 = $_POST['OrdermanagementUpdateForm']['foretell_type_12'];
            $model->foretell_type_13 = $_POST['OrdermanagementUpdateForm']['foretell_type_13'];
            $model->foretell_type_14 = $_POST['OrdermanagementUpdateForm']['foretell_type_14'];
            $model->foretell_type_15 = $_POST['OrdermanagementUpdateForm']['foretell_type_15'];
            $model->foretell_type_16 = $_POST['OrdermanagementUpdateForm']['foretell_type_16'];
            $model->foretell_type_17 = $_POST['OrdermanagementUpdateForm']['foretell_type_17'];
            $model->foretell_type_18 = $_POST['OrdermanagementUpdateForm']['foretell_type_18'];
            $model->foretell_type_19 = $_POST['OrdermanagementUpdateForm']['foretell_type_19'];
            $model->foretell_type_20 = $_POST['OrdermanagementUpdateForm']['foretell_type_20'];
            $model->foretell_type_21 = $_POST['OrdermanagementUpdateForm']['foretell_type_21'];
            $model->remark = $_POST['OrdermanagementUpdateForm']['remark'];
            $model->price_minimum = $_POST['OrdermanagementUpdateForm']['price_minimum'];
            $model->birthday = $_POST['OrdermanagementUpdateForm']['birthday'];
            $model->status = $_POST['OrdermanagementUpdateForm']['status'];
            if ($model->validate() && $model->foretell($order)) {
                $this->redirect(array('ordermanagement/index'));
            }
        }

        $Criteria = new CDbCriteria();
        $Criteria->condition = "active = true";
        $package = Package::model()->findAll($Criteria);
        $package_select = Package::model()->findByPk($order->package_id);

        $status = Status::model()->findAll();

        $this->render('updateorder', array('model' => $model, 'order' => $order, 'package' => $package, 'package_select' => $package_select, 'status' => $status));
    }

    public function actionDelete() {
        $id = $_GET["id"];
        if (empty($id)) {
            $this->redirect(array('ordermanagement/index'));
        }

        if (Order::model()->deleteByPk($id)) {
            if (!empty($order->picture) && strpos($order->picture, ImageUtil::PREFIX_FRIEND) > 0) {
                @unlink(Yii::app()->basePath . '/../' . $order->picture);
            }
            $this->redirect(array('ordermanagement/index'));
        }
    }

}
